package com.xrkol.sharelib.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 时间转换工具类
 * 
 * @author dipei.ruan
 *
 */
public class TimeUtil {

	/**
	 * 获取时间戳
	 * 
	 * @param 待转换date
	 * @return Long
	 */
	public static Long toTimestamp(Date date) {
		if (date != null) {
			return date.getTime();
		}
		return null;
	}

	/**
	 * 时间戳至Date
	 * 
	 * @param timestamp
	 * @return
	 */
	public static Date toDate(Long timestamp) {
		return new Date(timestamp);
	}

	/**
	 * 获取距离date参数spend天数后的日期
	 * 
	 * @param date
	 * @param spend
	 *            可为负数
	 * @return
	 */
	public static Date spendDate(Date date, int spend) {
		Long endTime = date.getTime() + spend * 24 * 60 * 60 * 1000;
		return new Date(endTime);
	}

	public static String getCurrentDateFormatString(String format) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);
		return dateFormat.format(new Date());
	}

	public static String getDateFormatString(Date date, String format) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);
		return dateFormat.format(date);
	}

	public static Date parseStringToDate(String dateStr, String format) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);
		Date date = null;
		try {
			date = dateFormat.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
}
